clear;clc;clf; close all;

%% 两对meta-atom
%%
mws = CST_Initilization;
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc), 'S']);
% CST_Parameter_Sweep(mws, 'Dx', -30, 30, 10);
% CST_Parameter_Sweep(mws, 'Dy', -20, 10, 5);
% invoke(invoke(mws, 'FDSolver'), 'Start');
% release(mws);
% release(cst);

%% 调用CSTStudio并初始化
function mws = CST_Initilization()
    % path = pwd; % 获取当前m文件夹路径
    cst = actxserver('CSTStudio.application');
    mws = invoke(cst, 'NewMWS');
    % app = invoke(mws, 'GetApplicationName');
    % ver = invoke(mws, 'GetApplicationVersion');
    invoke(mws, 'FileNew');
    invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行

end

%% 建模函数
function [] = CST_Modeling(mws)

    Frq = [0.5, 2]; % 工作频率,太赫兹波波长范围是3mm-30um
    FC_Property_Setup_Simplfied(mws, Frq,'time');
    % 仿真参数设置
    Px = 350; %结构X向边长
    Py = 350; %结构Y向边长
    t_metal = 0.2; %基地层金属厚度
    t_dielectric = 25;
    L1 = 120; W1 = 20; H1 = 50; Dx = 0; Sx = Px / 4;
    L2 = 120; W2 = 20; H2 = 50; Dy = 0; Sy = Py / 4;

    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'Px', Px);
    invoke(mws, 'StoreParameter', 'Py', Py);
    invoke(mws, 'StoreParameter', 't_metal', t_metal);
    invoke(mws, 'StoreParameter', 't_dielectric', t_dielectric);
    invoke(mws, 'StoreParameterWithDescription', 'L1 ', L1, ' ');
    invoke(mws, 'StoreParameterWithDescription', 'L2 ', L2, ' ');
    invoke(mws, 'StoreParameter', 'W1 ', W1);
    invoke(mws, 'StoreParameter', 'W2 ', W2);
    invoke(mws, 'StoreParameter', 'H1 ', H1);
    invoke(mws, 'StoreParameter', 'H2 ', H2);
    invoke(mws, 'StoreParameter', 'Dx ', Dx);
    invoke(mws, 'StoreParameter', 'Dy ', Dy);
    invoke(mws, 'StoreParameter', 'Sx ', Sx);
    invoke(mws, 'StoreParameter', 'Sy ', Sy);
    % CST_Post = CST_MicrowaveStudio();
    % CST_Post.addParameter('t', 100);

    % 组件
    FC_New_Component(mws, "Ground");
    FC_New_Component(mws, "介质");
    FC_New_Component(mws, "Metal");

    % 建立模型
    % 1. 建立底层基底金属模型
    % 建立方块的函数

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '-t_metal', '0', 'Gold_YW', 'Ground', '基底');

    % 2. 建立meta-atoms
    % 第一组meta-atom

    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', '-L1/2+W1', 'L1/2-W1', '0', 'W1', 'HTL_YW', 'Resin1', 'M');
    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', 'L1/2-W1', 'L1/2', '0', 'H1', 'HTL_YW', 'Resin1', 'U');
    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', '-L1/2', '-L1/2+W1', '0', 'H1', 'HTL_YW', 'Resin1', 'D');

    % 第二组meta-atom

    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', '-L2/2+W2+DY', 'L2/2-W2+DY', '0', 'W2', 'HTL_YW', 'Resin2', 'M');
    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', 'L2/2-W2+DY', 'L2/2+DY', '0', 'H2', 'HTL_YW', 'Resin2', 'U');
    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', '-L2/2+DY', '-L2/2+W2+DY', '0', 'H2', 'HTL_YW', 'Resin2', 'D');

    % 金属
    % 第一组meta-atom
    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', '-L1/2+W1', 'L1/2-W1', 'W1', 'W1+t_metal', 'Gold_YW', 'Metal1', 'M');
    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', 'L1/2-W1', 'L1/2', 'H1', 'H1+t_metal', 'Gold_YW', 'Metal1', 'U');
    FC_Model_Brick(mws, '-Sx-W1/2', '-Sx+W1/2', '-L1/2', '-L1/2+W1', 'H1', 'H1+t_metal', 'Gold_YW', 'Metal1', 'D');
    % 第一组meta-atom
    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', '-L2/2+W2+DY', 'L2/2-W2+DY', 'W2', 'W2+t_metal', 'Gold_YW', 'Metal2', 'M');
    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', 'L2/2-W2+DY', 'L2/2+DY', 'H2', 'H2+t_metal', 'Gold_YW', 'Metal2', 'U');
    FC_Model_Brick(mws, 'Sx+Dx-W2/2', 'Sx+Dx+W2/2', '-L2/2+DY', '-L2/2+W2+DY', 'H2', 'H2+t_metal', 'Gold_YW', 'Metal2', 'D');
end

%% 后处理函数
% function verticalRingModeling(mws, location, length, height, Width)
%     FC_Model_Brick(mws, 'location-width/2', 'location+width/2', '-length/2+Width', 'length/2-Width', '0', 'width', 'HTL_YW', 'Resin', 'M');
%     FC_Model_Brick(mws, 'location-width/2', 'location+width/2', 'length/2-Width', 'length/2', '0', 'height', 'HTL_YW', 'Resin', 'U');
%     FC_Model_Brick(mws, 'location-width/2', 'location+width/2', '-length/2', '-length/2+width', '0', 'height', 'HTL_YW', 'Resin', 'D');
% end

%% 保存文件
function [] = CST_Save_File(mws, Path, Filename)
    fullname = [Path Filename];
    invoke(mws, 'SaveAs', fullname, 'True'); %True表示保存到目前为止的结果
end

function [] = CST_Parameter_Sweep(mws, Para, From_, To_, StepWidth_)
    % Para = 'Dy'; From_ = 0; To_ = 12; StepWidth_ = 4;
    CMDs = '';
    CMDs = [CMDs 'With ParameterSweep'];
    CMDs = [CMDs 10 '  .DeleteAllSequences'];
    %     CMDs = [CMDs 10 '    .SetSimulationType ("Frequency")'];
    CMDs = [CMDs 10 ' .AddSequence ("Sweep_' Para '")'];
    CMDs = [CMDs 10 ' .AddParameter_Stepwidth ("Sweep_' Para '", "' Para '", ' num2str(From_) ', ' num2str(To_) ', ' num2str(StepWidth_) ')'];
    %     CMDs = [CMDs 10 '    .Start'];
    CMDs = [CMDs 10 'End With']; % CMDs
    invoke(mws, 'AddToHistory', ['定义参数化扫描_' Para], CMDs);

end
